<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input
          placeholder="请输入项目名称"
          prefix-icon="el-icon-search"
          v-model="search.project_name"
          @keyup.enter.native="SearchProject()">
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" icon="el-icon-search" round @click="SearchProject()">搜索</el-button>
      </el-col>
    </el-row>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="ID" min-width="5%" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" min-width="15%" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link @click="edit(scope.row.id)" style="color:#298abd">{{ scope.row.project_name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="发布应用" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.publish_app }}
        </template>
      </el-table-column>
      <el-table-column label="负责人" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.responsible_name }}
        </template>
      </el-table-column>
      <el-table-column label="测试人员" width="200" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.test_user }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="更新时间" label="更新时间" min-width="10%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ $common.formatDateTime(scope.row.update_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="8%" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-tooltip content="编辑" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.row.id)">
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row.id)">
              </el-button>
            </el-tooltip>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page_num"
      :page-sizes="[10, 20]"
      :page-size="pagination.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script>

import { getProjectList, deleteProject } from '@/api/project/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: {
        project_options: [],
        project_name: ''
      },
      pagination: {
        total: 0,
        page_current: 1,
        page_num: 1,
        page_size: 10
      },
      list: null,
      listLoading: true,
      ispagination: false
    }
  },
  created() {
    this.fetchProjectData()
  },

  methods: {
    fetchProjectData() {
      var params = {}

      params['ordering'] = '-update_time'
      this.listLoading = true
      getProjectList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
    },
    SearchProject: function() {
      var params = {}
      if (this.search.project_name) {
        params['search'] = this.search.project_name
      }

      if (this.ispagination){
        params['page_num'] = this.pagination.page_num
        params['page_size'] = this.pagination.page_size
      }else{
        params['page_num'] = 1
        params['page_size'] = this.pagination.page_size
      }
      params['ordering'] = '-update_time'
      this.listLoading = true
      getProjectList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
      if (this.ispagination == false){
        this.pagination.page_num = 1
       }
      this.ispagination = false
    },

    handleSizeChange: function(val) {
      this.pagination.page_size = val
      this.SearchProject()
    },
    handleCurrentChange: function(val) {
      this.pagination.page_num = val
      this.ispagination = true
      this.SearchProject()
    },
    edit(arg){
      this.$router.push({
        path:'/project/update_project',
        query:{
          id: arg
			}
		})
    },

    del(arg){
      this.$confirm('此操作将删除项目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
      var params = {}
      params['id'] = arg
      deleteProject(params).then(response => {
        if (response.status == "000000") return
          this.SearchProject()
      })
        this.$message({
          type: 'success',
          message: '删除成功!',
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
